package com.project.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.music.bean.Comment;
import com.project.music.service.CommentService;
import com.project.music.utils.Consts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 评论控制类
 */

@Api(tags = "评论控制类")
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    CommentService commentService;

    //提交评论
    @ApiOperation("提交评论")
    @PostMapping("/add")
    public JSONObject addComment(Comment comment) {
        JSONObject jsonObject = new JSONObject();

        comment.setCreateTime(new Date());
        boolean status = commentService.save(comment);
        if (status) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "评论成功");
        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "评论失败");
        }
        return jsonObject;
    }
    //获取所有评论
    @ApiOperation("获取所有评论")
    @GetMapping("")
    public List<Comment> allComment() {
        return commentService.list();
    }
    //获得指定歌曲ID的评论列表
    @ApiOperation("获得指定歌曲ID的评论列表")
    @GetMapping("/song/detail")
    public List<Comment> commentOfSongId(@RequestParam("songId") @ApiParam("歌曲ID") int songId) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .eq(Comment::getSongId, songId);
        return commentService.list(queryWrapper);
    }
    //获得指定歌单ID的评论列表
    @ApiOperation("获得指定歌单ID的评论列表")
    @GetMapping("/songList/detail")
    public List<Comment> commentOfSongListId(@RequestParam("songListId") @ApiParam("歌单ID") int songListId) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Comment::getSongListId, songListId);
        return commentService.list(queryWrapper);
    }
    //点赞
    @ApiOperation("点赞")
    @PostMapping("/like")
    public JSONObject like(@RequestParam("id") @ApiParam("评论ID") int id,
                       @RequestParam("up") @ApiParam("点赞数") int up) {
        JSONObject jsonObject = new JSONObject();

        Comment comment = new Comment();
        comment.setId(id);
        comment.setUp(up);
        boolean status = commentService.updateById(comment);
        if (status) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "点赞成功");
        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "点赞失败");
        }
        return jsonObject;
    }
    //删除评论
    @ApiOperation("删除评论")
    @GetMapping("/delete")
    public Boolean deleteComment(@RequestParam("id") @ApiParam("评论ID") int id) {
        return commentService.removeById(id);
    }
    //更新评论
    @ApiOperation("更新评论")
    @PostMapping("/update")
    public JSONObject updateComment(Comment comment) {
        JSONObject jsonObject = new JSONObject();

        boolean status = commentService.updateById(comment);
        if (status) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "修改成功");
        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "修改失败");
        }
        return jsonObject;
    }
}
